package pub.elephants.socket.netty.client;

import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.SimpleChannelInboundHandler;
import pub.elephants.net.body.ElephantsCallBack;
import pub.elephants.net.body.ElephantsResponse;


public class NettyClientHandler extends SimpleChannelInboundHandler<ElephantsResponse> {

    @Override
    protected void channelRead0(ChannelHandlerContext channelHandlerContext, ElephantsResponse elephantsResponse) throws Exception {
        ElephantsCallBack future = ElephantsCallBack.requestPool.get(elephantsResponse.getRequestId());
        future.setElephantsResponse(elephantsResponse);
        ElephantsCallBack.requestPool.put(elephantsResponse.getRequestId(),future);
    }

    @Override
    public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
        ctx.close();
    }
}
